/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.daolayer;

import fri.businessLayer.UserBeanRemote;
import fri.businessLayer.UserInRoleBeanRemote;
import fri.entiry.FrRole;
import fri.entiry.FrUser;
import fri.entiry.FrUserinrole;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author NguyenDongUIT
 */
public class UserDAO {
    
    public static FrUser getUserByCode(String userCode) {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.getUserByCode(userCode);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    public static List<FrUser> getAllUser() {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.getAllUser();

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static FrUser getUserByUserEmail(String email)
    {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.getUserByEmail(email);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static FrUser getUserByUserId(String userId) {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.getUserByUserId(userId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static boolean checkLogin(FrUser user) {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.checkLogin(user);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return false;
    }
    
    public static boolean checkLoginByEmail(String email, String password)
    {
        try {
            FrUser user=new FrUser();
            user.setEmail(email);
            user.setPassword(password);
            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.checkLogin(user);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return false;
    }
    
    public static boolean checkLoginByUserCode(String userCode, String password)
    {
        try {
            FrUser user=new FrUser();
            user.setUserCode(userCode);
            user.setPassword(password);
            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.checkLogin(user);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return false;
    }
    
    public static List<FrUser> getUserByFullName(String fullName)
    {
        try {

            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            return rm.getUserByFullName(fullName);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static int addUser(String email, String fullName, String avatar, String password)
    {
        
        try {

            UUID u=UUID.randomUUID();
            String userId=u.toString();
            
            UUID i=UUID.randomUUID();
            
            String userCode=i.toString();
            userCode=userCode.substring(0, 8);
            FrUser user=new FrUser();
            user.setUserId(userId);
            user.setEmail(email);
            user.setFullName(fullName);
            user.setUserCode(userCode);
            user.setAvatar(avatar);
            user.setPassword(password);
            user.setDateCreated(new Date());
            user.setLastEffect(new Date());
            
            Context ic = new InitialContext();
            UserBeanRemote rm = (UserBeanRemote) ic.lookup("fri.businessLayer.UserBeanRemote");
            UserInRoleBeanRemote rn = (UserInRoleBeanRemote) ic.lookup("fri.businessLayer.UserInRoleBeanRemote");
            FrUserinrole userinrole=new FrUserinrole();
            userinrole.setFrUser(user);
            userinrole.setFrRole(new FrRole("1"));
            userinrole.setDateCreated(new Date());
            rn.addUserInRole(null);
            return rm.addUser(user);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
        
        
        
    }
    
}
